const mixin_formItemType = require('../../../../common/mixins/formItemType')
const mixin_dicKey = require('../../../../common/mixins/dicKey');
const {
  WxValidate
} = require('../../../../utils/WxValidate');
const {
  WorkOrderMyIndexApi
} = require('../../../../common/apis/workOrder');
const proposeObj = {
  organizer: '',
  phone: '',
  sponsor: '',
}
Component({
  behaviors: [mixin_formItemType, mixin_dicKey],

  /**
   * 组件的属性列表
   */
  properties: {
    orderNum: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    //  涉及系统
    aboutSystem: '',
    //  附件
    enclosure: [],
    //  期望完成日期
    endTime: '',
    //  紧急程度
    level: '',
    //  科室
    proposeList: [proposeObj],
    //  需求描述
    remark: '',
    //  提出日期
    startTime: '',
    //  需求标题
    title: '',
    rules: {
      title: {
        required: true
      },
      remark: {
        required: true
      },
      aboutSystem: {
        required: true
      },
      level: {
        required: true
      },
      startTime: {
        required: true
      },
    },

    message: {
      title: {
        required: '请输入需求标题'
      },
      remark: {
        required: '请输入需求描述'
      },
      aboutSystem: {
        required: '请选择涉及系统'
      },
      level: {
        required: '请选择优先级'
      },
      startTime: {
        required: '请选择提出日期'
      },

    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 提出人
    onChangeInputUser(e) {
      const index = e.currentTarget.dataset.index;
      const {
        value
      } = e.detail
      this.setData({
        [`proposeList[${index}].sponsor`]: value
      });
    },
    //电话
    onChangeInputPhone(e) {
      const index = e.currentTarget.dataset.index;
      const {
        value
      } = e.detail
      this.setData({
        [`proposeList[${index}].phone`]: value
      });
    },
    // 科室
    onSelectDept(e) {

      const index = e.currentTarget.dataset.index;
      const value = e.detail.value.value;

      this.setData({
        [`proposeList[${index}].organizer`]: value
      });
    },
    // 添加科室
    onAdd() {
      const {
        proposeList
      } = this.data
      proposeList.push(proposeObj)
      this.setData({
        proposeList: proposeList
      })
    },
    // 附件上传
    onUploadData(e) {
      const {
        value,
        type
      } = e.detail
      if (type === 'fileList') {
        this.setData({
          enclosure: value
        })
      } else {
        let {
          enclosure
        } = this.data
        enclosure.push(...value)
        this.setData({
          enclosure: enclosure
        })
      }
    },
    submit(callback) {
      console.log(this.data, 'submit');
      const {
        aboutSystem,
        enclosure,
        endTime,
        level,
        orderNum,
        proposeList,
        remark,
        startTime,
        title,
      } = this.data
      const params = {
        aboutSystem,
        enclosure,
        endTime,
        level,
        orderNum,
        proposeList,
        remark,
        startTime,
        title,
      }

      const validate = new WxValidate(this.data.rules, this.data.message)
      if (!validate.checkForm(params)) {
        const error = validate.errorList[0]
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
        return false
      }
      const rules = {
        organizer: {
          required: true

        },
        phone: {
          required: true

        },
        sponsor: {
          required: true

        }
      }
      const message = {
        organizer: {
          required: '请选择科室'

        },
        sponsor: {
          required: '请输入提出人'
        },
        phone: {
          required: '请输入电话'
        },
      }
      const validate1 = new WxValidate(rules, message)
      for (let i of params.proposeList) {
        if (!validate1.checkForm(i)) {
          const error = validate1.errorList[0]
          wx.showToast({
            title: error.msg,
            icon: 'error'
          })
          return false
        }
      }

      WorkOrderMyIndexApi.upgrade(params, ({
        code,
        msg
      }) => {
        if (code === 200) {
          wx.showToast({
            title: '处理成功',
            icon: "success"
          })
          wx.$_.delay(callback, 1000)

        } else {
          console.error(msg);
          wx.showToast({
            title: msg,
            icon: 'error'
          })
        }
      })

    },
  },
})